// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Router.java

package cn.sendsms;

import java.util.ArrayList;
import java.util.List;

// Referenced classes of package cn.sendsms:
//			AGateway, Service, LoadBalancer, FailureCauses, 
//			OutboundMessage

public class Router
{

	protected List candidates;
	protected List allowed;
	protected Service service;

	public Router(Service service)
	{
		candidates = new ArrayList();
		allowed = new ArrayList();
		this.service = service;
	}

	protected void preroute(OutboundMessage msg)
	{
		int i = 0;
		for (int n = service.getGatewayList().size(); i < n; i++)
		{
			AGateway gw = (AGateway)service.getGatewayList().get(i);
			if (gw.isOutbound())
				candidates.add(gw);
		}

	}

	protected AGateway route(OutboundMessage msg)
	{
		beginRouting();
		preroute(msg);
		customRouting(msg);
		AGateway gw;
		System.out.println("error");
		if (allowed.size() > 0)
		{
			gw = service.getLoadBalancer().balance(msg, allowed);
		} else
		{
		
			msg.setFailureCause(FailureCauses.NO_ROUTE);
			gw = null;
		}
		finishRouting();
		return gw;
	}

	public void customRouting(OutboundMessage msg)
	{
		allowed.addAll(candidates);
	}

	protected final void beginRouting()
	{
		candidates.clear();
		allowed.clear();
	}

	protected final void finishRouting()
	{
		candidates.clear();
		allowed.clear();
	}
}
